package org.example.myleet.rosalind.qrt;

import org.example.myleet.rosalind.utils.Quartet;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Solution {
    public static void main(String[] args) throws IOException {
        Solution solution = new Solution();
        String path = solution.getClass().getResource("").getPath() + "example.txt";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)))) {
            String line = br.readLine().trim();
            String[] elements = line.split(" ");
            Set<Quartet> quartets = new HashSet<>();
            while (null != (line = br.readLine())) {
                line = line.trim();
                if (line.length() < 1) {
                    break;
                }
                findAllQuartets(elements, line, quartets);
            }
            for (Quartet quartet : quartets) {
                System.out.println(quartet.toString());
            }
        }
    }

    private static class Pair {
        String s0;
        String s1;

        public Pair(String s0, String s1) {
            this.s0 = s0;
            this.s1 = s1;
        }
    }

    private static void findAllQuartets(String[] elements, String partialCharacter, Set<Quartet> quartets) {
        //根据partialCharacter将elements分开
        List<String> part0 = new ArrayList<>();
        List<String> part1 = new ArrayList<>();
        for (int i = 0; i < elements.length; ++i) {
            char c = partialCharacter.charAt(i);
            if (c == '1') {
                part1.add(elements[i]);
            } else if (c == '0') {
                part0.add(elements[i]);
            }
        }
        if (part0.size() < 2 || part1.size() < 2) {
            return;
        }
        //然后分别进行两两组合得到Pair，然后再进行Pair的两两组合
        List<Pair> part0Pairs = new ArrayList<>(part0.size() * (part0.size() - 1));
        for (int i = 0; i < part0.size(); ++i) {
            for (int j = i + 1; j < part0.size(); ++j) {
                part0Pairs.add(new Pair(part0.get(i), part0.get(j)));
            }
        }
        List<Pair> part1Pairs = new ArrayList<>(part1.size() * (part1.size() - 1));
        for (int i = 0; i < part1.size(); ++i) {
            for (int j = i + 1; j < part1.size(); ++j) {
                part1Pairs.add(new Pair(part1.get(i), part1.get(j)));
            }
        }
        for (Pair part0Pair : part0Pairs) {
            for (Pair part1Pair : part1Pairs) {
                //注意去重
                quartets.add(new Quartet(part0Pair.s0, part0Pair.s1, part1Pair.s0, part1Pair.s1));
            }
        }
    }
}
